package com.zjljr.order.service.impl;

import com.zjljr.common.entity.order.Order;
import com.zjljr.order.feign.StockServiceFeign;
import com.zjljr.order.mapper.OrderMapper;
import com.zjljr.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceIml implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    StockServiceFeign stockServiceFeign;

    @Override
    //@Transactional
    @GlobalTransactional
    public Order addOrder(Integer productId) {
        //添加订单
        Order order = new Order();
        order.setProductId(productId);
        order.setTotalAmount(100*productId);
        orderMapper.insert(order);

        //扣减库存
        stockServiceFeign.reduceStock(productId);

        //抛出异常，看2个服务是否都回滚
        int i = 1/0;

        return order;
    }
}
